﻿using CommonData.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CToolsWin
{
    public partial class ShowStr : Form
    {
        public bool isSuccess = true;
        public ShowStr(string resStr, StringType type)
        {
            InitializeComponent();
            try
            {
                switch (type)
                {
                    case StringType.Json:
                        txtHtml.Text = GetJsonStr(resStr);
                        break;
                    case StringType.Xml:
                        txtHtml.Text = resStr;
                        break;
                    default:
                        txtHtml.Text = resStr;
                        break;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                txtHtml.Text = "格式有误," + ex.Message;
            }
        }

        private string GetJsonStr(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "字符串不能为空";
            }
            str = str.Trim();
            if (str.StartsWith("[") && str.EndsWith("]"))
            {
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = Common.Serialize.JsonHelper.ToObject<List<Dictionary<string, object>>>(str);
                string resStr = "[";
                foreach (var item in list)
                {
                    resStr += "\r\n\t{";
                    resStr += string.Join(",", item.Select(kv => "\r\n\t\t\"" + kv.Key + "\":" + ObjToJsonStr(kv.Value, "\t")));
                    resStr += "\r\n\t},";
                }
                resStr = resStr.TrimEnd(',') + "\r\n]";
                return resStr;
            }
            else if (str.StartsWith("{") && str.EndsWith("}"))
            {
                Dictionary<string, object> list = new Dictionary<string, object>();
                list = Common.Serialize.JsonHelper.ToObject<Dictionary<string, object>>(str);
                string resStr = "{";
                resStr += string.Join(",", list.Select(item => "\r\n\t\"" + item.Key + "\":" + ObjToJsonStr(item.Value, "\t")));
                resStr = resStr.TrimEnd(',') + "\r\n}";
                return resStr;
            }
            else
            {
                isSuccess = false;
                return "格式有误";
            }
        }
        private string ObjToJsonStr(object obj, string tabStr)
        {
            if (obj is int || obj is long || obj is decimal || obj is bool)
            {
                return obj.ToString();
            }
            else if (obj is Array || obj is Newtonsoft.Json.Linq.JArray || obj is Newtonsoft.Json.Linq.JObject)
            {
                string str = obj.ToString();
                str = str.Trim();
                if (str.StartsWith("[") && str.EndsWith("]"))
                {
                    List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                    list = Common.Serialize.JsonHelper.ToObject<List<Dictionary<string, object>>>(str);
                    string resStr = "[";
                    foreach (var item in list)
                    {
                        resStr += "\r\n" + tabStr + "\t\t{";
                        resStr += string.Join(",", item.Select(kv => "\r\n" + tabStr + "\t\t\t\"" + kv.Key + "\":" + ObjToJsonStr(kv.Value, tabStr + "\t")));
                        resStr += "\r\n" + tabStr + "\t\t},";
                    }
                    resStr = resStr.TrimEnd(',') + "\r\n" + tabStr + "\t]";
                    return resStr;
                }
                else if (str.StartsWith("{") && str.EndsWith("}"))
                {
                    Dictionary<string, object> list = new Dictionary<string, object>();
                    list = Common.Serialize.JsonHelper.ToObject<Dictionary<string, object>>(str);
                    string resStr = "{";
                    resStr += string.Join(",", list.Select(item => "\r\n" + tabStr + "\t\t\"" + item.Key + "\":" + ObjToJsonStr(item.Value, tabStr + "\t")));
                    resStr += "\r\n" + tabStr + "\t}";
                    return resStr;
                }
                else
                {
                    throw new Exception("格式有误");
                }
            }
            else
            {
                return "\"" + obj.ToString() + "\"";
            }
        }
    }
}
